import Vue from 'vue'
import VueRouter from 'vue-router'
import index from "../layout/index";
import Home from "../views/Home"
import store from "../store/index"
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    component: index,
    children:[
      {
        path:'/',
        component:Home
      },
      {
        path: "/detailCourse",
        component: ()=>import("../views/detailCourse")
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: "/index22",
    component: ()=>import('../layout/index2'),
    children:[
      {
        path:'/',
        component: ()=>import("../views/detailCourse")
      },
      {
        path:'shopCar',
        component: ()=>import("../views/shopCar"),
        meta:{
          IsLogin:true
        }
      },
      {
        path: "userInfo",
        component: ()=>import("../views/userInfo")
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
let getLoginsta = store.dispatch("getLoginsta")
router.beforeEach((to, from, next) => {
  if(to.meta.IsLogin){
    getLoginsta.then(()=>{
      if(store.state.login){
        next();
      }else{
        next("/")
        store.commit("change",true);

      }
    })
  }else {
    next();
  }
})

export default router
